package com.wfh.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.wfh.R;
import com.wfh.bean.ProductBean;
import com.wfh.util.MyEditText;
import com.wfh.util.MyEditText.OnChangeListener;
import com.wfh.util.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class OrderAdapter extends BaseAdapter {

	Context context;
	public ArrayList<ProductBean> list;
	private Map<String, SoftReference<Bitmap>> tempcache = new HashMap<String, SoftReference<Bitmap>>();
	private ListView lv;
	private int status = 0;

	// private static HashMap<Integer, Boolean> IsSelected = new
	// HashMap<Integer, Boolean>();

	public OrderAdapter(Context context, ArrayList<ProductBean> list,
			ListView lv) {
		this.context = context;
		this.list = list;
		this.lv = lv;
		lv.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_IDLE:// 停止
					status = 0;
					OrderAdapter.this.notifyDataSetChanged();
					break;
				case OnScrollListener.SCROLL_STATE_FLING:
					status = 1;
					break;
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					status = 2;
					break;
				default:
					break;
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

			}
		});

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		View adapter;
		// Log.i("view","getView " + position + " " + view);
		LayoutInflater minflater = LayoutInflater.from(context);

		adapter = minflater.inflate(R.layout.adapter_order, null);
		TextView product_name = (TextView) adapter.findViewById(R.id.textView1);
		TextView price = (TextView) adapter.findViewById(R.id.textView2);
		ImageView imageview = (ImageView) adapter.findViewById(R.id.imageView1);
		imageview.setImageResource(R.drawable.nopic);
		product_name.setText(list.get(position).getProduct_name());
		price.setText(list.get(position).getProduct_price() + "元");
		// 设置点菜
		final MyEditText edit = (MyEditText) adapter
				.findViewById(R.id.edittext1);
		String number = list.get(position).getNumber() == null ? "0" : list
				.get(position).getNumber();
		edit.setNumber(number);

		if (tempcache.containsKey(list.get(position).getProduct_img())
				&& tempcache.get(list.get(position).getProduct_img()).get() != null) {
			imageview.setImageBitmap(tempcache.get(
					list.get(position).getProduct_img()).get());

		} else {
			if (status == 0) {// 解决图片加载太多卡屏问题,触摸滚动停止才加载图片
				loadDrawable(list.get(position).getProduct_img(), imageview,
						new ImageCallback() {
							@Override
							public void imageloaded(Bitmap bitmap,
									ImageView imageview, String imgurl) {
								imageview.setImageBitmap(bitmap);
								// OrderAdapter.this.notifyDataSetChanged();
							}
						});
			}
		}

		// 记录点菜数量
		edit.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange() {
				list.get(position).setNumber(edit.getNumber());
			}
		});

		return adapter;
	}

	/*
	 * public static HashMap<Integer,Boolean> getIsSelected(){ return
	 * IsSelected; }
	 */

	public void loadDrawable(final String imageUrl, final ImageView imageView,
			final ImageCallback imageCallback) {

		final Handler handler = new Handler() {
			public void handleMessage(Message message) {
				imageCallback.imageloaded((Bitmap) message.obj, imageView,
						imageUrl);
			}
		};

		new Thread() {
			public void run() {
				Bitmap bitmap = Util.getBitmap(imageUrl, 3);
				tempcache.put(imageUrl, new SoftReference<Bitmap>(bitmap));// 二级缓存
				Message message = handler.obtainMessage(0, bitmap);
				handler.sendMessage(message);
			}
		}.start();
	}

	// imagecallback
	public interface ImageCallback {
		public void imageloaded(Bitmap bitmap, ImageView imageview,
				String imgurl);
	}

	public void clearImgcache() {
		if (tempcache != null || tempcache.size() > 0) {
			Set<String> keys = tempcache.keySet();
			for (Iterator it = keys.iterator(); it.hasNext();) {
				String s = (String) it.next();
				Bitmap bmp = tempcache.get(s).get();
				if (bmp != null) {
					if (bmp.isRecycled() == false) {
						bmp.recycle();
					}
					bmp = null;
				}
			}
			tempcache.clear();
			System.gc();
		}
	}

	public void refresh(ArrayList<ProductBean> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
